<?php
class folder_item_language extends Model{
	public static function getTableName(){
		return 'folder_item-language';
	}

	/*
	 * by donald
	 */
    public function getPrimaryKey()
    {
        return array('folder_id', 'item_id', 'language_id');
    }

	/*
	 * by donald
	 */
	public function getAllByType($type, $item_id)
	{
		switch (strtolower($type)) {
			case 'image':
				$folder_item_type_id = AC::FOLDER_TYPE_IMAGE;
				break;
			case 'video':
				$folder_item_type_id = AC::FOLDER_TYPE_VIDEO;
				break;
		}

		$item_folders = 'SELECT id FROM folder WHERE folder_item_type_id=' . $folder_item_type_id;
		$sql = ' 
			SELECT * 
			FROM `folder_item-language` 
			WHERE item_id= ' . $item_id . " AND folder_id IN($item_folders)";
		return self::$dbo['w']->findBySql(__CLASS__, $sql);
	}

	/*
	 * by donald
	 * modified at 2011-09-22
	 */
	public static function getAll(array $params)
	{
		foreach ($params as $field => $value) {
			switch ($field) {
				case 'folder_id':
					$where[] = 'folder_id = ?';
					$where_array[] = $value;
					break;
				case 'item_id':
					$where[] = 'item_id = ?';
					$where_array[] = $value;
					break;
			}
		}

		return self::$dbo['w']->find(__CLASS__, implode(' AND ', $where), $where_array);
	}

	/*
	 * by donald
	 * at 2011-09-22
	 */
	public static function addFolderItemLanguageChangeLog($folder_id, $item_id, $programme_id, $episode_id, $is_active)
	{
		$folder_item_language_list = self::getAll(
			array(
				'folder_id' => $folder_id,
				'item_id' => $item_id
			)
		);

		foreach ($folder_item_language_list as $folder_item_language) {
			$folder_item_language->add_change_log($programme_id, $episode_id, $is_active);
		}
	}

	/*
	 * by donald
	 * at 2011-09-22
	 */
	public function add_change_log($programme_id, $episode_id, $is_active)
	{
		$field['type'] = $this->getTableName();
		$field['keys'] = array(
			'folder_id' => $this->folder_id,
			'item_id' => $this->item_id,
			'language_id' => $this->language_id,
		);
		$field['is_active'] = $is_active; 
		$field['programme_id'] = $programme_id;
		$field['episode_id'] = $episode_id; 
		$field['is_publish'] = 0;

		change_log::updateOne($field);
	}

	/*
	 * by donald
	 */
	public function delete_change_log()
	{
		$type = $this->getTableName();
		$keys = array(
			'folder_id' => $this->folder_id,
			'item_id' => $this->item_id,
			'language_id' => $this->language_id,
		);
		$is_publish = 0;

		change_log::delTypeAndKeysLog($type, $keys, $is_publish);
	}

}
